<?php
require_once '../inc/simpleimage.php';
define('path', '/fotos/editor/foto%s');
// Required: anonymous function reference number as explained above.
$funcNum = $_GET['CKEditorFuncNum'] ;
// Optional: instance name (might be used to load a specific configuration file or anything else).
$CKEditor = $_GET['CKEditor'] ;
// Optional: might be used to provide localized messages.
$langCode = $_GET['langCode'] ;

//$name = $this->Mysql->getLast('fotos','id, enlace');
//$rest = substr($name['enlace'], -4);
//$rest++;
$file_name = 'foto';

$image = new SimpleImage();
$image->load($_FILES['upload']['tmp_name']);

if($image->getWidth() > $image->getHeight()){
	$image->resizeToWidth(800);
} else {
	$image->resizeToHeight(500);
}
$i = 0;
do {
	$name = $i . $_FILES['upload']['name'];
	$path = sprintf(path, preg_replace("/[^a-z0-9.]+/i", "", $name));
$i++;
} while (file_exists('..' . $path));
$image->save('..' . $path);

// Usually you will only assign something here if the file could not be uploaded.
$message = '';
 
echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '$path', '$message');</script>";
?>
